#ifndef HTTPRESPONSE_H
#define HTTPRESPONSE_H

#include <string>
#include <unordered_map>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include "../buffer/buffer.h"
#include "../log/log.h"

class HttpResponse {
public:
    enum class HttpStatusCode {
        OK = 200,           // HTTP 请求成功
        BAD_REQUEST = 400,  // 客户端请求不能被服务器所识别
        FORBIDDEN = 403,    // 客户端请求的资源被服务器拒绝访问
        NOT_FOUND = 404,    // 客户端请求的资源不存在
    };
public:
    HttpResponse();
    ~HttpResponse();

    void init(const std::string& srcDir, std::string& path, 
                bool isKeepAlive, int code);
    void makeResponse(Buffer& buff);

    void unmapFile();
    char* file();
    size_t fileLen() const;
    
    void errorContent(Buffer& buff, std::string message);
    int getCode() const;
private:
    void addResponeStateLine(Buffer& buff);
    void addResponeHeader(Buffer& buff);
    void addResponeContent(Buffer& buff);

    void errorHtml();
    std::string getFileType();
private:
    int code_;       // 响应码
    bool isKeepAlive_;          // 长连接

    std::string path_;          // 请求的文件路径
    std::string srcDir_;        // 请求的资源目录

    char* mmFile_;              // 文件映射的内容
    struct stat mmFileStat_;    // 映射文件的状态

    static const std::unordered_map<std::string, std::string> suffixType_;
    static const std::unordered_map<int, std::string> codeStatus_;
    static const std::unordered_map<int, std::string> codePath_;
};

#endif